<template>
    <el-dialog
            :title="!dataForm.id ? '新增' : '修改'"
            :close-on-click-modal="false"
            :visible.sync="visible">
        <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="120px">
                            <el-form-item label="主键ID" prop="id">
                        <el-input v-model.number="dataForm.id" placeholder="主键ID"></el-input>
                </el-form-item>
                <el-form-item label="金融资产ID" prop="assetId">
                        <el-input v-model.number="dataForm.assetId" placeholder="金融资产ID"></el-input>
                </el-form-item>
                <el-form-item label="股票价格" prop="stockPrice">
                        <el-input v-model="dataForm.stockPrice" placeholder="股票价格"></el-input>
                </el-form-item>
                <el-form-item label="汇率" prop="exchangeRate">
                        <el-input v-model="dataForm.exchangeRate" placeholder="汇率"></el-input>
                </el-form-item>
                <el-form-item label="利率" prop="interestRate">
                        <el-input v-model="dataForm.interestRate" placeholder="利率"></el-input>
                </el-form-item>
                <el-form-item label="风险价值（VaR）" prop="varValue">
                        <el-input v-model="dataForm.varValue" placeholder="风险价值（VaR）"></el-input>
                </el-form-item>
                <el-form-item label="GDP增长率" prop="gdpGrowthRate">
                        <el-input v-model="dataForm.gdpGrowthRate" placeholder="GDP增长率"></el-input>
                </el-form-item>
                <el-form-item label="通货膨胀率" prop="inflationRate">
                        <el-input v-model="dataForm.inflationRate" placeholder="通货膨胀率"></el-input>
                </el-form-item>
                <el-form-item label="失业率" prop="unemploymentRate">
                        <el-input v-model="dataForm.unemploymentRate" placeholder="失业率"></el-input>
                </el-form-item>
                <el-form-item label="系统性市场风险预警标志（TRUE为高风险）" prop="systemicRiskAlert">
                        <el-input v-model="dataForm.systemicRiskAlert" placeholder="系统性市场风险预警标志（TRUE为高风险）"></el-input>
                </el-form-item>
                <el-form-item label="创建时间" prop="createdAt">
                        <el-date-picker
                                v-model="dataForm.createdAt"
                                type="datetime"
                                placeholder="创建时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="更新时间" prop="updatedAt">
                        <el-date-picker
                                v-model="dataForm.updatedAt"
                                type="datetime"
                                placeholder="更新时间">
                        </el-date-picker>
                </el-form-item>
                <el-form-item label="压力测试结果" prop="stressTestResult">
                        <el-input v-model="dataForm.stressTestResult" placeholder="压力测试结果"></el-input>
                </el-form-item>
        </el-form>
        <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
    </el-dialog>
</template>

<script>
    export default {
      data () {
        return {
          visible: false,
          dataForm: {
            id: null,
            assetId: null,
            stockPrice: null,
            exchangeRate: null,
            interestRate: null,
            varValue: null,
            gdpGrowthRate: null,
            inflationRate: null,
            unemploymentRate: null,
            systemicRiskAlert: null,
            createdAt: null,
            updatedAt: null,
            stressTestResult: null
          },
          dataRule: {
            id: [
                        { required: true, message: '主键ID不能为空', trigger: 'blur' }
            ],
            assetId: [
                        { required: true, message: '金融资产ID不能为空', trigger: 'blur' }
            ],
            stockPrice: [
                        { required: true, message: '股票价格不能为空', trigger: 'blur' }
            ],
            exchangeRate: [
                        { required: true, message: '汇率不能为空', trigger: 'blur' }
            ],
            interestRate: [
                        { required: true, message: '利率不能为空', trigger: 'blur' }
            ],
            varValue: [
                        { required: true, message: '风险价值（VaR）不能为空', trigger: 'blur' }
            ],
            gdpGrowthRate: [
                        { required: true, message: 'GDP增长率不能为空', trigger: 'blur' }
            ],
            inflationRate: [
                        { required: true, message: '通货膨胀率不能为空', trigger: 'blur' }
            ],
            unemploymentRate: [
                        { required: true, message: '失业率不能为空', trigger: 'blur' }
            ],
            systemicRiskAlert: [
                        { required: true, message: '系统性市场风险预警标志（TRUE为高风险）不能为空', trigger: 'blur' }
            ],
            createdAt: [
                        { required: true, message: '创建时间不能为空', trigger: 'blur' }
            ],
            updatedAt: [
                        { required: true, message: '更新时间不能为空', trigger: 'blur' }
            ],
            stressTestResult: [
                        { required: true, message: '压力测试结果不能为空', trigger: 'blur' }
            ]
          }
        }
      },
      methods: {
        init (id) {
          this.dataForm.id = id || 0
          this.visible = true
          this.$nextTick(() => {
            this.$refs['dataForm'].resetFields()
            this.dataForm.id = id || 0
            if (this.dataForm.id) {
              this.$http({
                url: this.$http.adornUrl(`/app/market_risk/info/${this.dataForm.id}`),
                method: 'get',
                params: this.$http.adornParams()
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.dataForm = data.appmarketrisk
                }
              })
            }
          })
        },
            // 表单提交
        dataFormSubmit () {
          this.$refs['dataForm'].validate((valid) => {
            if (valid) {
              this.$http({
                url: this.$http.adornUrl(`/app/market_risk/${!this.dataForm.id ? 'save' : 'update'}`),
                method: 'post',
                data: this.$http.adornData(this.dataForm)
              }).then(({data}) => {
                if (data && data.code === 0) {
                  this.$message({
                    message: '操作成功',
                    type: 'success',
                    duration: 1500,
                    onClose: () => {
                      this.visible = false
                      this.$emit('refreshDataList')
                    }
                  })
                } else {
                  this.$message.error(data.msg)
                }
              })
            }
          })
        }
      }
    }
</script>